<?php
class subfaktur_model extends CI_Model{
	private $NoSubFaktur;
	private $Datesubfaktur;
	private $NoFaktur;
    private $Namacustomer;
	private $Quantity;
    private $Amount;
	private $Status;
  	private $Keterangan;
	private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setNoSubFaktur($NoSubFaktur){
		$this->NoSubFaktur = $NoSubFaktur;
	}
	function getNoSubFaktur(){
		return $this->NoSubFaktur;
	}
	
	function setDatesubfaktur($Datesubfaktur){
		$this->Datesubfaktur = $Datesubfaktur;
	}
	function getDatesubfaktur(){
		return $this->Datesubfaktur;
	}
	
	function setNoFaktur($nofaktur){
		$this->NoFaktur = $nofaktur;
	}
	function getNoFaktur(){
		return $this->NoFaktur;
	}
	
	function setNamacustomer($Namacustomer){
		$this->Namacustomer = $Namacustomer;
	}
	function getNamacustomer(){
		return $this->Namacustomer;
	}
	
	function setQuantity($QTY){
		$this->Quantity = $QTY;
	}
	function getQuantity(){
		return $this->Quantity;
	}
		
	function setAmount($amount){
		$this->Amount = $amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function setStatus($Status){
		$this->Status = $Status;
	}
	function getStatus(){
		return $this->Status;
	}
		
	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}

	function getCreatedBy(){
		return $this->createdby;
	}
	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}

	function getCreatedOn(){
		return $this->createdon;
	}
	
	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->lastupdateby;
	}
	
	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = $active_val;
	}
	function getActive(){
		return $this->active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getkodenofak()
	{
		$query = $this->db->query("select MAX(RIGHT(NoSubFaktur,4)) as kdsubfaktur from subfaktur_product_penjualan");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idsubfaktur)
			{
				$tmpsubfaktur = ((int)$idsubfaktur->kdsubfaktur)+1;
				$kd = sprintf("%04s", $tmpsubfaktur);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd="SFK".$kd;
		return $kd;
	}
		
	function insert()
	{
		$data = array(
			'NoSubFaktur' => $this->getNoSubFaktur(),
			'NoFaktur' => $this->getNoFaktur(),
			'Datesubfaktur' => $this->getDatesubfaktur(),
			'Namacustomer' => $this->getNamacustomer(),
			'Quantity' => $this->getQuantity(),
			'Amount' => $this->getAmount(),
			'Status' => $this->getStatus(),
			'Keterangan' => $this->getKeterangan(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => 'null',
			'Lastupdateon' => 'null',
			'Active' => '1'						
		);
		$this->db->insert('subfaktur_product_penjualan',$data);
	}
	
	function update()
	{
		$data = array(
			'Namacustomer' => $this->getNamacustomer(),
			'Quantity' => $this->getQuantity(),
			'Amount' => $this->getAmount(),
			'Status' => $this->getStatus(),
			'Keterangan' => $this->getKeterangan(),
			'Lastupdateby' => $this->getLastupdateby(),
			'Lastupdateon' => $this->getLastupdateon()
                
		);
 
        $this->db->where(array('NoSubFaktur' => $this->getNoSubFaktur()));
        $this->db->update('subfaktur_product_penjualan', $data);		
	}
		
	function deletesubfaktur() 
	{
		
        $data = array(
			'Status' => $this->getStatus(),
            'Active' => '0'
        );
        $this->db->where(array('NoSubFaktur' => $this->getNoSubFaktur()));
        $this->db->update('subfaktur_product_penjualan', $data);
        
        //var_dump($this->db->last_query());exit;
    }
	
	
	function getTotalqty()
	{
		
		$query = $this->db->query("select Quantity as totalquant from subfaktur_product_penjualan
			where NoSubFaktur='". $this->getNoSubFaktur() ."'");
		$row = $query->row();
		return $row->totalquant;
	}

	function ambilamountsubfak() {
        $this->db->select("Amount")
       	->where(array('subfaktur_product_penjualan.NoSubFaktur' => $this->getNoSubFaktur()));
        $query = $this->db->get('subfaktur_product_penjualan');
        return $query->result();
    }
	
	function cekamountsubfak()
	{	
		$query = $this->db->query("SELECT Amount FROM subfaktur_product_penjualan WHERE NoSubFaktur = '".$this->getNoSubFaktur()."'");
		$amtsubfak = "0";
		
		foreach($query->result() as $d)
		{
			$amtsubfak = $d->Amount;
		}
		return $amtsubfak;		
	}
}